package com.msite.config;

import com.msite.service.rabbitmq.ContentReceive;
import com.msite.service.rabbitmq.MessageReceive;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tpeng on 2015/12/30.
 */
@Configuration
@ConditionalOnExpression("false")
public class RabbitMQConfig {

    public final static String MQKEY = "rmq_";

    public final static String QUEUE_MESSAGE = MQKEY + "message_queue";
    public final static String QUEUE_CONTENT = MQKEY + "content_queue";


    private final String DIRECT_EXCHANGE_NAME = MQKEY + "exchange";

    @Autowired
    private ConnectionFactory rabbitConnectionFactory;

   /* @Bean
    Binding exchangeBinding() {
        return BindingBuilder.bind(new Queue(QUEUE_MESSAGE, true))
                .to(new DirectExchange(MQKEY + "message_exchange", true, false)).with(MQKEY + "routing.key.message");
    }

    @Bean
    Binding contentExchangeBinding() {
        return BindingBuilder.bind(new Queue(QUEUE_CONTENT, true))
                .to(new DirectExchange(MQKEY + "content_exchange", true, false)).with(MQKEY + "routing.key.content");
    }*/

    /*
    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate r = new RabbitTemplate(rabbitConnectionFactory);
        r.setExchange(MQKEY + "message_exchange");
        r.setRoutingKey(MQKEY + "routing.key.message");
        return r;
    }
    */

    /*@Bean
    MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(messageReceiveListener(), "receiveMessage");
    }*/

    @Bean
    SimpleMessageListenerContainer messageScheduler() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory);
        container.setQueueNames(QUEUE_MESSAGE);
        container.setMessageListener(messageReceiveListener());
        return container;
    }

    @Bean
    SimpleMessageListenerContainer contentScheduler() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory);
        container.setQueueNames(QUEUE_CONTENT);
        container.setMessageListener(contentReceiveListener());
        return container;
    }

    @Bean
    MessageReceive messageReceiveListener() {
        return new MessageReceive();
    }

    @Bean
    ContentReceive contentReceiveListener() {
        return new ContentReceive();
    }

}
